<?php 

/**
 * Joomla! 1.5 component arts_curriculum
 * @package    arts_curriculum
 * @subpackage
 * @author     Chris Borkowski {@link http://www.guggenheim.org}
 * @author     Created on 10-January-2010
 * @author     Last Modified on 16-July-2010
 */
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.view');


/**
* HTML View class for the arts_curriculum component
* @package Arts_curriculum
*/
	  class Arts_curriculumViewSort extends JView
	  {
		function display( $tpl = null )
		{
			global $mainframe, $option;
		 	
		 	
		 	// Initialize some variables
		    $document =& JFactory::getDocument();
		  	$document->addStyleSheet($this->baseurl.'/components/com_arts_curriculum/css/ac_front.css');
		  	$uri =& JFactory::getURI();
		  	$pathway =& $mainframe->getPathway();
		 
		 	// Get the parameters of the active menu item
		  	$menus =& JSite::getMenu();
		  	$menu = $menus->getActive();
		 
		 	// Get some data from the model
		  	$items =& $this->get('data' );
		  	$note =& $this->get('note' );
		  	$total =& $this->get('total');
		  	$pagination =& $this->get('pagination');
		  	$category =& $this->get('category' );
		  	$state =& $this->get('state');
		
			//get the categories model, so we can use it also here
		 	$model =& JModel::getInstance('categories', 'arts_curriculummodel');
		  	$categories =& $model->getData();
		 	
		 	// Get the page/component configuration
		  	$params =& $mainframe->getParams();
		 	$category->total = $total;
		 	
		 	// sort stuff
		 	$search =& JFactory::getURI();
		    $search->getVar('sortby', 'There are no tems in this subject or medium', 'post','string');
		 	 	
		 	//load & process system content plugins ! note we are running it on the category array
			$dispatcher =& JDispatcher::getInstance();
			JPluginHelper::importPlugin('content');
			$results = $dispatcher->trigger('onPrepareContent', array (& $category, & $params, 1));
		 	
		 	// Add alternate feed link
		  	if($params->get('show_feed_link', 1) == 1)
		  	{
		  	$link = '&view=category&id='.$category->slug.'&format=feed&limitstart=';
		  	$attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0');
		  	$document->addHeadLink(JRoute::_($link.'&type=rss'), 'alternate', 'rel', $attribs);
		  	$attribs = array('type' => 'application/atom+xml', 'title' => 'Atom 1.0');
		  	$document->addHeadLink(JRoute::_($link.'&type=atom'), 'alternate', 'rel', $attribs);
		  	}
		 
		 	
		 	$menus =& JSite::getMenu();
		  	$menu = $menus->getActive();
		 	
		 	// because the application sets a default page title, we need to get it
		  	// right from the menu item itself
		  	if (is_object( $menu )) {
		  	$menu_params = new JParameter( $menu->params );
		  	if (!$menu_params->get( 'page_title')) {
		  	$params->set('page_title', $category->title);
		  	}
		  	} else {
		  	$params->set('page_title', $category->title);
		  	}
		  	$document->setTitle( $params->get( 'page_title' ) );
		 
		 	
		 	//set breadcrumbs
		  	if(is_object($menu) && $menu->query['view'] != 'category') {
		  	$pathway->addItem($category->title, '');
		  	}
		 
		 	// Prepare category description
		  	$category->description = JHTML::_('content.prepare', $category->description);
		 	// table ordering
		  	$lists['order_Dir'] = $state->get('filter_order_dir');
		  	$lists['order'] = $state->get('filter_order');
		 	
		 	// Set some defaults if not set for params
		  	$params->def('comp_description', JText::_('ARTS_CURRICULUM_DESC'));
		  
		  
		  	// Define image tag attributes
		  	if (isset( $category->image ) && $category->image != '')
		  	{
		  	$attribs['align'] = $category->image_position;
		  	$attribs['hspace'] = 6;
		 	
		 	// Use the static HTML library to build the image tag
		  	$category->image = JHTML::_('image', 'images/stories/'.$category->image, JText::_('ARTS CURRICULUM'), $attribs);
		  	}
		 	
		 	// icon in table display
		  	if ( $params->get( 'link_icons' ) <> -1 ) {
		  	$image = JHTML::_('image.site', $params->get('link_icons', 'weblink.png'), '/images/M_images/', $params->get( 'weblink_icons' ), '/images/M_images/', 'Link' );
		  	}
		 	
		 	$k = 0;
		  	$tick=1;
		  	$count = count($items);
		  	
		  	//problem artists names on artist sort -these will need to be maintained by a human - sorry!!!
		  	//special condition 1 - Artist name not in first part of Exhibition name
		  	$artist_names = array ('Frank Lloyd Wright', 'Daniel Buren' );
		  	
		  	//special condition 2 - artist name in 2 or exhibitions
		  	$artist_names_exhibitions = array ('Vasily Kandinsky','El Greco');
		  	
		  	//special condition 3 - artist name in 2 or exhibitions and themes
		  	$artist_names_themes = array ('Pablo Picasso');
		  	
		  	
		  	//begin loop to form things we will need out of the raw SQL query and pushed back into the $this->items array
		  	for($i = 0; $i < $count; $i++)
		  	{
		 	$item =& $items[$i];
		 
	
		 	$link = JRoute::_( 'index.php?view=item&catid='.$item->catID.'&id='. $item->id);
		 	
		  	//$menuclass = 'category'.$params->get( 'pageclass_sfx' );
		 	//$itemParams = new JParameter($item->params);
		  	
		  	$item->link = '<a href="'. $link .'" class="ac_sort_article_link">Theme: '.$this->escape($item->title) .'</a><br>
		  	<div class="ac_sort_lesson_areas_box">'.Arts_curriculumFrontHelper::subjectlinks(& $item->subject).'</div>';  
			
			//we need to make a special category link
			$catlink = JRoute::_('index.php?option=com_arts_curriculum&view=category&id='. $item->catID);
			$item->catlink = '<div class="ac_sort_cat_link"><a href="'. $catlink .'" class="ac_sort_cat_link">Exhibition: '.$this->escape($item->catTITLE).'</a></div>';    
		 	
		 	//Artist name link
		 	$item->artist_name_link = '<div class="ac_spaced"><a href="'. $link .'" class="ac_artist_sort_name_link">'.$this->escape($item->artist_name) .'&nbsp;'.$this->escape($item->artist_bio).'</a></div>'; 
    		
		 	//link to Exhibition Theme for most artists in artist sort
		 	$item->artist_less_or_ex_link = '<a href="'. $link .'" class="ac_artist_sort_ex_link" id="normal_'.$this->artist_name.$k++.'">Exhibition: '.$this->escape($item->catTITLE) .'</a><br>'  ;

		 	//check for and make linkable image if it doesn't exist
		 	Arts_curriculumFrontHelper::checkderivative2($item->crop);
			$item->catThumbnail = '<a href="'. $catlink .'"class="ac_sort_img_link"><img src="/images/content/arts_curriculum/derivative2/'.$item->crop.'" alt="'. $this->escape($item->catTITLE).'" width="120" height="76"/></a>';
		 	
		 	//we need a special links for the ARTISTS SORT - there are 5 known conditions that the SQL query can't deal with
		 	//compare artist name to exhibiton title
		 	if (strcmp($item->artist_name, $item->catTITLE) < -2) {
      		$item->artist_name_link = '<div class="ac_spaced"><a href="'. $catlink .'" class="ac_artist_sort_name_link">'.$this->escape($item->artist_name) .'&nbsp;'.$this->escape($item->artist_bio).'</a></div>'; 
     		$item->artist_less_or_ex_link = '<a href="'. $catlink .'" class="ac_artist_sort_ex_link">Exhibition: '.$this->escape($item->catTITLE) .'</a><br>';
		 	
		 	//evaluate artist name to exhibiton title without artist name
		 	}elseif  (in_array($item->artist_name, $artist_names, true)) {
    		$item->artist_name_link = '<div class="ac_spaced"><a href="'. $catlink .'" class="ac_artist_sort_name_link">'.$this->escape($item->artist_name) .'&nbsp;'.$this->escape($item->artist_bio).'</a></div>'; 
		 	$item->artist_less_or_ex_link = '<a href="'. $catlink .'" class="ac_artist_sort_ex_link">Exhibition: '.$this->escape($item->catTITLE) .'</a><br>';
		 	
		 	//special condition where artist appear in one or more exhibitions 
		 	}elseif  (in_array($item->artist_name, $artist_names_exhibitions ,true )) {
		 	//link to exhibiitons view ?
    		$item->artist_name_link = '<div class="ac_spaced"><span class="ac_artist_sort_name_link">'.$this->escape($item->artist_name) .'&nbsp;'.$this->escape($item->artist_bio).'</span></div>'; 
		 	$item->artist_less_or_ex_link = '<a href="'. $catlink .'" class="ac_artist_sort_ex_link" >Exhibition: '.$this->escape($item->catTITLE) .'</a><br>';
		
			//special condition where artist appear in one or more exhibitions & themes
		 	}elseif  (in_array($item->artist_name, $artist_names_themes ,true )) {
		 	//link to exhibiitons view ?
    		$item->artist_name_link = '<div class="ac_spaced"><span class="ac_artist_sort_name_link">'.$this->escape($item->artist_name) .'&nbsp;'.$this->escape($item->artist_bio).'</span></div>'; 
		 	$item->artist_less_or_ex_link = '<a href="'. $catlink .'" class="ac_artist_sort_ex_link" >Exhibition: '.$this->escape($item->catTITLE) .'</a><br/>';
			}	
				
		 	
		 	$item->image = $image;
		 	//make alternateding numbers
		 	$item->odd = $k;
		  	$item->count = $i;
		  	$k = 1 - $k;
		  	
		  	}// the loop ends here	
		 	
		 	//we need a special array for the artists sort after all the links are made
		 	//$artists = Arts_curriculumFrontHelper::cleansort($this->$items);
		 
			//well never need it - let's dump this in the final release		 
		 	$count = count($categories);
		  	for($i = 0; $i < $count; $i++)
		  	{
		  	$cat =& $categories[$i];
		  	$cat->link = JRoute::_('index.php?option=com_arts_curriculum&view=category&id='. $cat->slug);
		  	}
		 
		 	// push items into the default view 
		 	$this->assignRef('lists', $lists);
		  	$this->assignRef('params', $params);
		  	$this->assignRef('category', $category);
		 	$this->assignRef('categories', $categories);
		  	$this->assignRef('items', $items);
		  	$this->assignRef('note', $note);
		  	//$this->assignRef('artists', $artists);
		  	$this->assignRef('pagination', $pagination);
		 
		 	parent::display($tpl);
		  	}
	
	}